<?php

/**
 *		//用户类//
 *		//这是一份试验品，由大街编写（zhongmingzi@gmail.com）
 *		//本程序默认环境：Zend Optimizer Yes / 3.3.3
 *		//		Apache/2.0.63 (Win32) PHP/5.2.12
 *		//		MySQL 支持 Yes / client lib version 5.0.89
 *		//		GD library Yes / bundled (2.0.34 compatible)
 *		//		eAccelerator/Memcache/xcache Yes
 *		//		cURL support enabled
 *		//时间：2011 3 25 08:45:23//
 */
if(!defined('IN')) {
	exit('Access Denied');
}

class member {

	function checkname($name, $type = 0) {
		if($type == 0) $c = 'username';
		else $c = 'email';
		$sql = "SELECT COUNT(uid) FROM ".DB::t('u_members')." WHERE $c = '$name'";
		return DB::result_first($sql);
	}

	function register($email, $username, $pw, $pwsec, $login = true) {
		global $_G;
		//已经登录
		//if($_G['uid'] != 0) return -1;
		if($email && $username && $pw && $pwsec ) {
			$username = trim($username);
			$pripw = $pw;
			$pw = md5($pw);
			$pwsec = md5($pwsec);
			//两次密码不同
			if($pw !=$pwsec) return -2;
			$email = trim($email);
			//email格式错误
			if(!isemail($email)) return -3;
			
			$reg_user_arr = array(
				'username' => $username,
				'email' => $email,
				'password' => $pw,
				'groupid' => 1,
				'regip' => $_G['ip'],
				'regdate' => TIMESTAMP,
				'lastip' => $_G['ip'],
				'lastvisit' => TIMESTAMP,
				'lastactivity' => TIMESTAMP,
			);
			//注册失败，用户已经存在
			if(!$uid = DB::insert('u_members', $reg_user_arr, true, false, true)) return -4;
			$reg_pro_arr = array(
				'uid'=>$uid
			);
			DB::insert('u_members_pro', $reg_pro_arr);
			
			if($login == true) {
				$lab = & core::instance();
				$lab->session->set('uid', $uid);
				$lab->session->set('username', $username);
				dsetcookie('auth', authcode("$pw\t$uid", 'ENCODE'), 92000, 1, true);
			}
			return 1;
		} else {
			//资料不完整
			return -5;
		}
	}
	
	function login($email, $pw, $remember = false) {
		global $_G;
		if($_G['uid']) return 1;
		if(!empty($email) && !empty($pw)) {
			$email = trim($email);
			$pripw = $pw;
			$pw = md5($pw);
			//Email格式错误
			if(!isemail($email)) return -1;
			$result = DB::fetch_first("SELECT username,uid FROM ".DB::t('u_members')." WHERE email='$email' AND password='$pw'");
			
			if($result['uid']) {
				$lab = & core::instance();
				$lab->session->set('uid', $result['uid']);
				$lab->session->set('username', $result['username']);
				$life = $remember == true ? 2592000 : 0;
				dsetcookie('auth', authcode("$pw\t{$result['uid']}", 'ENCODE'), $life, 1, true);
				return $result['uid'];
			} else {
				$u = DB::fetch_first("SELECT uid,username FROM ".DB::t('u_members')." WHERE email = '$email'");
				if(!empty($u) && is_array($u)) {
					$log = array(
						"{$u['uid']}_{$u['username']}" => TIMESTAMP."\t $pripw \t".$_G['ip']
					);
					writelog('failedlogin', $log);
				}
				//密码或用户名错误
				return -2;
			}
		} else {
			//填写信息不完整
			return -3;
		}
	}
	
	function logout($formhash) {
		if($formhash != FROMHASH) return 0;
		dsetcookie('auth', '');
		return 1;
	}
	
	function chpw($uid, $newpw, $newpwsec = '', $oldpw = '') {
		global $_G;
		//权限不够
		if($_G['uid'] != $uid && $_G['adminid'] < 5) return -1;
		if(!($_G['adminid'] >= 5 && $_G['uid'] != $uid)) {
			//输入不完整
			if(empty($newpw) || empty($newpwsec) || empty($oldpw)) return -2;
			//两次输入不完整
			if($newpw != $newpwsec) return -3;
			//旧密码检测
			if(md5($oldpw) != DB::result_first("SELECT password FROM ".DB::t('u_members')." WHERE uid = '$uid'")) return -4;
		}

		$newpw = md5($newpw);
		DB::update('u_members', array('password' => $newpw), " uid='$uid'");
		return 1;
	}
	
	function chavatar($name) {
		global $_G;
		if(!$_G['uid']) return -1;
		$num = 10000000 + $_G['uid'];
		$n1 = substr($num, 6);
		$n2 = substr($num, 4, 2);
		$n3 = substr($num, 1, 3);
		$path = "avatar/".$n3."/".$n2."/".$n1;
		if(!file_exists(ROOT.'/data/'.$path)) {
			if(!dmkdir(ROOT.'/data/'.$path)) return -2;
		}
		if($_FILES[$name]['error'] == 4) return -3;
		if($_FILES[$name]['error'] == 2) return -4;
		$allow = array(
			"image/gif",
			"image/jpeg",
			"image/pjpeg",
			"image/jpg",
			"image/png"
		);
		if(!in_array($_FILES[$name]['type'], $allow)) return -5;
		$fileext = fileext($_FILES[$name]['name']);
		
		include_once libfile('class/image');
		$realfilename = $path."/".$_G['uid'].'.'.$fileext;
		$image = new image($_FILES[$name]['tmp_name']);
		$sign1 = $image->thumb($realfilename, 200, 0, 0);
		$sign2 = $image->thumb($realfilename, 40, 1, 1);
		if($sign1 && $sign2) {
			$sql = "UPDATE ".DB::t('u_members')." SET avatar = '$realfilename' WHERE uid = '{$_G['uid']}'";
			DB::exec($sql);
			return 1;
		} else {
			return -6;
		}
	}
	
	function chmember() {
		global $_G;
		//这里填写不能使用的域名
		$domain_arr = $_G['setting']['reserved'];
		
		$user_arr['timeoffset'] = $_G['gp_timeoffset'] == $_G['member']['timeoffset'] ? null : htmlspecialchars($_G['gp_timeoffset']);
		
		$userpro_arr['nickname'] = $_G['gp_mnickname'] == $_G['member']['nickname'] ? null : htmlspecialchars($_G['gp_mnickname']);
		$userpro_arr['gender'] = $_G['gp_gender'] == $_G['member']['gender'] ? null : htmlspecialchars(intval($_G['gp_gender']));
		$userpro_arr['birthyear'] = $_G['gp_birthyear'] == $_G['member']['birthyear'] ? null : htmlspecialchars(intval($_G['gp_birthyear']));
		$userpro_arr['birthmonth'] = $_G['gp_birthmonth'] == $_G['member']['birthmonth'] ? null : htmlspecialchars(intval($_G['gp_birthmonth']));
		$userpro_arr['birthday'] = $_G['gp_birthday'] == $_G['member']['birthday'] ? null : htmlspecialchars(intval($_G['gp_birthday']));
		$userpro_arr['singht'] = $_G['gp_msinght'] == $_G['member']['singht'] ? null : htmlspecialchars($_G['gp_msinght']);
		$userpro_arr['qq'] = $_G['gp_mqq'] == $_G['member']['qq'] ? null : htmlspecialchars($_G['gp_mqq']);
		$userpro_arr['phone'] = $_G['gp_phone'] == $_G['member']['phone'] ? null : htmlspecialchars($_G['gp_phone']);
		$userpro_arr['site'] = $_G['gp_site'] == $_G['member']['site'] ? null : htmlspecialchars($_G['gp_site']);
		//$userpro_arr['domain'] = $_G['gp_domain'] == $_G['member']['domain'] ? null : (in_array($_G['gp_domain'], $domain_arr) ? null : $_G['gp_domain']);
		//$userpro_arr['address'] = $_G['gp_address'] == $_G['member']['address'] ? null : htmlspecialchars($_G['gp_address']);
		//$userpro_arr['postcode'] = $_G['gp_postcode'] == $_G['member']['postcode'] ? null : htmlspecialchars(intval($_G['gp_postcode']));
		
		$userpro_arr['isemail'] = (($is = $_G['gp_isemail'] == 'on' ? '1': '0') == $_G['member']['isemail']) ? null : ($_G['gp_isemail'] == 'on' ? '1': '0');
		$userpro_arr['isbirth'] = (($is = $_G['gp_isbirth'] == 'on' ? '1': '0') == $_G['member']['isbirth']) ? null : ($_G['gp_isbirth'] == 'on' ? '1': '0');
		$userpro_arr['iscity'] = (($is = $_G['gp_iscity'] == 'on' ? '1': '0') == $_G['member']['iscity']) ? null : ($_G['gp_iscity'] == 'on' ? '1': '0');
		$userpro_arr['isphone'] = (($is = $_G['gp_isphone'] == 'on' ? '1': '0') == $_G['member']['isphone']) ? null : ($_G['gp_isphone'] == 'on' ? '1': '0');
		$userpro_arr['isqq'] = (($is = $_G['gp_isqq'] == 'on' ? '1': '0') == $_G['member']['isqq']) ? null : ($_G['gp_isqq'] == 'on' ? '1': '0');
		//$userpro_set_arr['isaddress'] = (($is = $_G['gp_isaddress'] == 'on' ? '1': '0') == $_G['member']['isaddress']) ? null : ($_G['gp_isaddress'] == 'on' ? '1': '0');
		
		foreach($user_arr as $user => $userv) {
			if($userv == null) unset($user_arr[$user]);
		}
		foreach($userpro_arr as $user => $userv) {
			if($userv == null) unset($userpro_arr[$user]);
		}
		if(!empty($user_arr)) {
			DB::update('u_members', $user_arr, "uid = '{$_G['uid']}'");
		}
		if(!empty($userpro_arr)) {
			DB::update('u_members_pro', $userpro_arr, "uid = '{$_G['uid']}'");
		}
		return 1;
	}
	
	function showbirthday($type = 'y') {
		global $_G;
		if($type == 'y'){
			$string .= '<select id="birthyear" name="birthyear"><option value="">年</option>\n\r';
			for($i = date('Y'); $i >= 1949; $i--){
				$str = $_G['member']['birthyear'] == $i ? 'selected=\"selected\"' : '';
				$string .= "<option value=\"$i\" $str >$i</option>\n\r\\t\t\t";
			}
			$string .= "</select>";
		}elseif($type == 'm'){
			$string .= '<select id="birthmonth" name="birthmonth"><option value="">月</option>\n\r';
			for($i = 1; $i <= 12; $i++){
				$str = $_G['member']['birthmonth'] == $i ? 'selected=\"selected\"' : '';
				$string .= "<option value=\"$i\" $str >$i</option>/n/r/t/t/t/t";
			}
			$string .= "</select>";
		}elseif($type == 'd'){
			$string .= '<select id="birthday" name="birthday"><option value="">日</option>\n\r';
			for($i = 1; $i <= 31; $i++){
				$str = $_G['member']['birthday'] == $i ? 'selected=\"selected\"' : '';
				$string .= "<option value=\"$i\" $str >$i</option>/n/r/t/t/t/t";
			}
			$string .= "</select>";
		}
		return $string;
	}

	function showgender() {
		global $_G;
		$str0 = $_G['member']['gender'] == 0 ? 'selected=\"selected\"' : '';
		$str1 = $_G['member']['gender'] == 1 ? 'selected=\"selected\"' : '';
		$str2 = $_G['member']['gender'] == 2 ? 'selected=\"selected\"' : '';
		$string .= "<select name=\"gender\" id=\"gender\">\n\r<option value=\"0\" $str0 >保密</option>\n\r<option value=\"1\" $str1 >男</option>\n\r<option value=\"2\" $str2 >女</option>\n\r</select>";
		return $string;
	}

	function showtimeoffset() {
		global $_G;
		include_once libfile('member/timeoffset', 'include');
		$string .= '<select name="timeoffset" style="width:300px;">\n\r<option value="9999">'.$timeoffset['9999'].'</option>\n\r';
		for($i = -12; $i <= 12; $i++){
			$str = $_G['member']['timeoffset'] == $i ? 'selected=\"selected\"' : '';
			$string .= "<option value=\"$i\" $str >".$timeoffset[$i]."</option>/n/r/t/t/t/t";
			if($i == -4){
				$str = $_G['member']['timeoffset'] == -3.5 ? 'selected=\"selected\"' : '';
				$string .= "<option value=\"-3.5\" $str >".$timeoffset['-3.5']."</option>/n/r/t/t/t/t";
			}elseif($i == 3){
				$str = $_G['member']['timeoffset'] == 3.5 ? 'selected=\"selected\"' : '';
				$string .= "<option value=\"3.5\" $str >".$timeoffset['3.5']."</option>/n/r/t/t/t/t";
			}elseif($i == 4){
				$str = $_G['member']['timeoffset'] == 4.5 ? 'selected=\"selected\"' : '';
				$string .= "<option value=\"4.5\" $str >".$timeoffset['4.5']."</option>/n/r/t/t/t/t";
			}elseif($i == 5){
				$str = $_G['member']['timeoffset'] == 5.5 ? 'selected=\"selected\"' : '';
				$string .= "<option value=\"5.5\" $str >".$timeoffset['5.5']."</option>/n/r/t/t/t/t";
				$str = $_G['member']['timeoffset'] == 5.75 ? 'selected=\"selected\"' : '';
				$string .= "<option value=\"5.75\" $str >".$timeoffset['5.75']."</option>/n/r/t/t/t/t";
			}elseif($i == 6){
				$str = $_G['member']['timeoffset'] == 6.5 ? 'selected=\"selected\"' : '';
				$string .= "<option value=\"6.5\" $str >".$timeoffset['6.5']."</option>/n/r/t/t/t/t";
			}elseif($i == 9){
				$str = $_G['member']['timeoffset'] == 9.5 ? 'selected=\"selected\"' : '';
				$string .= "<option value=\"9.5\" $str >".$timeoffset['9.5']."</option>/n/r/t/t/t/t";
			}
		}
		$string .= "</select>";
		return $string;
	}
}

